#!/usr/bin/env python3
# minitest -m newscase -c config.json -g
import minium
import time
import os
import random
class NewsTest(minium.MiniTest):
    def test_news(self):
        def test_scroll_to(page, heightpx):
            heightpx += random.randint(250, 550)
            page.scroll_to(heightpx,500)
            time.sleep(1)
            # self.assertEqual(page.scroll_y, heightpx, "scroll success")
            return page.scroll_y

        def test_screen_shot(index):
            output_path = os.path.join(os.path.dirname(__file__), "outputs/images/shot_%d.png" %index)
            if not os.path.isdir(os.path.dirname(output_path)):
                os.mkdir(os.path.dirname(output_path))
            if os.path.isfile(output_path):
                os.remove(output_path)
            ret = self.app.screen_shot(output_path)
            # self.assertTrue(os.path.isfile(output_path))


        newspage = self.app.switch_tab("/pages/news/news")
        heightpx = 0
        for _ in range(8):
            time.sleep(1)
            self.assertEqual(self.app.current_page.path, "/pages/news/news", "news path here")
            test_screen_shot(heightpx)
            heightpx = test_scroll_to(newspage, heightpx)
            i = heightpx // 256 + random.randint(1, 2)
            e = self.page.get_element(selector="/page/view/view[%s]" %i)
            e.tap()
            time.sleep(1)
            self.assertTrue(self.app.wait_for_page("/pages/extern/extern"), "arrive news[%s]" %i)
            self.assertEqual(self.app.current_page.path, "/pages/extern/extern", "extern path here")
            test_screen_shot(heightpx)
            self.app.navigate_back()

        
